package com.skt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.skt.domain.PO.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 检查用户名是否存在（排除指定用户ID）
     */
    @Select("SELECT COUNT(*) FROM user WHERE username = #{username} AND id != #{excludeId}")
    boolean existsByUsernameExcludeId(@Param("username") String username, @Param("excludeId") Long excludeId);

    /**
     * 检查邮箱是否存在（排除指定用户ID）
     */
    @Select("SELECT COUNT(*) FROM user WHERE email = #{email} AND id != #{excludeId}")
    boolean existsByEmailExcludeId(@Param("email") String email, @Param("excludeId") Long excludeId);

    /**
     * 检查手机号是否存在（排除指定用户ID）
     */
    @Select("SELECT COUNT(*) FROM user WHERE phone = #{phone} AND id != #{excludeId}")
    boolean existsByPhoneExcludeId(@Param("phone") String phone, @Param("excludeId") Long excludeId);

    /**
     *  通过商家注册申请
     * @param userId
     * @param expectedOldStatus
     * @param newStatus
     * @param approvedAt
     * @param approvedBy
     * @return
     */
    int updateMerchantStatusToApproved(
            @Param("userId") Long userId,
            @Param("expectedOldStatus") Integer expectedOldStatus,
            @Param("newStatus") Integer newStatus,
            @Param("approvedAt") LocalDateTime approvedAt,
            @Param("approvedBy") Long approvedBy
    );


    /**
     *  拒绝商家注册申请
     * @param userId
     * @param expectedOldStatus
     * @param newStatus
     * @param rejectedAt
     * @param rejectedBy
     * @return
     */
    int updateMerchantStatusToReject(
            @Param("userId") Long userId,
            @Param("expectedOldStatus") Integer expectedOldStatus,
            @Param("newStatus") Integer newStatus,
            @Param("rejectedAt") LocalDateTime rejectedAt,
            @Param("rejectedBy") Long rejectedBy,
            @Param("rejectionReason") String rejectionReason
    );

    /**
     * 分页查询用户名（用于布隆过滤器预热）
     * @param offset 偏移量
     * @param limit 每页大小
     * @return 用户名列表
     */
    @Select("SELECT username FROM user ORDER BY id LIMIT #{limit} OFFSET #{offset}")
    List<String> selectUsernamesBatch(@Param("offset") int offset, @Param("limit") int limit);


}